package com.dongnao.jack.service.store;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.dongnao.jack.registry.handler.JedisServiceConfigBean;
import com.dongnao.jack.spring.parse.ProtocolConfig;

public class RedisParseService implements ParseService {

    @Override
    public ProtocolConfig parseProtocol(String registerInfo) {

        if (StringUtils.isNoneBlank(registerInfo)) {
            JedisServiceConfigBean bean = JSONObject.parseObject(registerInfo, JedisServiceConfigBean.class);

            return bean.getProtocol();
        }

        return null;
    }

    @Override
    public String parseRef(String registerInfo) {

        if (StringUtils.isNoneBlank(registerInfo)) {
            JedisServiceConfigBean bean = JSONObject.parseObject(registerInfo, JedisServiceConfigBean.class);

            return bean.getRef();
        }
        return null;
    }

    @Override
    public String parseHost(String registerInfo) {

        if (StringUtils.isNoneBlank(registerInfo)) {
            JedisServiceConfigBean bean = JSONObject.parseObject(registerInfo, JedisServiceConfigBean.class);

            return bean.getHostAndIp().split(":")[0];
        }
        return null;
    }

    @Override
    public int parsePort(String registerInfo) {

        if (StringUtils.isNoneBlank(registerInfo)) {
            JedisServiceConfigBean bean = JSONObject.parseObject(registerInfo, JedisServiceConfigBean.class);

            return Integer.parseInt(bean.getHostAndIp().split(":")[1]);
        }

        return 0;
    }

}
